package com.atguli.gulimall.product.service.impl;

import com.atguli.common.entity.QueryWrapperVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguli.common.utils.PageUtils;
import com.atguli.common.utils.Query;

import com.atguli.gulimall.product.dao.AttrGroupDao;
import com.atguli.gulimall.product.entity.AttrGroupEntity;
import com.atguli.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
@Slf4j
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(QueryWrapperVO queryWrapperVO) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(queryWrapperVO),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * @param queryWrapperVO
     * @param catelogId
     */
    @Override
    public PageUtils queryPage(QueryWrapperVO queryWrapperVO, Long catelogId) {
        log.info("进入商品属性分组查询");
        if (catelogId == 0) {
            final IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(queryWrapperVO),
                    new QueryWrapper<AttrGroupEntity>());
            log.info("商品属性分组查询结束");
            return new PageUtils(page);
        }else {
            final QueryWrapper<AttrGroupEntity> condition = new QueryWrapper<AttrGroupEntity>();
            condition.eq("catelog_id",catelogId);
            if (StringUtils.isNotEmpty(queryWrapperVO.getKey())) {
                log.info("进入关键字查询");
                final String key = queryWrapperVO.getKey();
                condition.and((item) -> {
                            item.like("attr_group_id",key)
                                    .or()
                                    .like("attr_group_name",key);
                        });
            }
            final IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(queryWrapperVO), condition);
            log.info("商品属性分组查询结束");
            return new PageUtils(page);
        }
    }

}